package com.ruoyi.project.wenyanwen.generate.controller;

import com.ruoyi.common.utils.DbGenerateUtil;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.wenyanwen.generate.domain.DbGenerate;
import com.ruoyi.project.wenyanwen.generate.service.IDbGenerateService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 数据包生成Controller
 *
 * @author shixiangwen
 * @date 2020-09-27
 */
@Controller
@RequestMapping("/wenyanwen/generate")
public class DbGenerateController extends BaseController {

    private final static String PROGRESS_TEMPLATE = "<div class='progress-bar %1$s' role='progressbar' aria-valuenow='%2$d' aria-valuemin='0' aria-valuemax='100' style='width: %2$d%%;'>%2$d%%</div>";

    private String prefix = "wenyanwen/generate";

    @Autowired
    private IDbGenerateService dbGenerateService;

    @RequiresPermissions("wenyanwen:generate:view")
    @GetMapping()
    public String generate() {
        return prefix + "/generate";
    }

    /**
     * 查询数据包生成列表
     */
    @RequiresPermissions("wenyanwen:generate:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(DbGenerate dbGenerate) {
        startPage();
        List<DbGenerate> list = dbGenerateService.selectDbGenerateList(dbGenerate);
        return getDataTable(list);
    }

    /**
     * 导出数据包生成列表
     */
    @RequiresPermissions("wenyanwen:generate:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(DbGenerate dbGenerate) {
        List<DbGenerate> list = dbGenerateService.selectDbGenerateList(dbGenerate);
        ExcelUtil<DbGenerate> util = new ExcelUtil<DbGenerate>(DbGenerate.class);
        return util.exportExcel(list, "generate");
    }

    /**
     * 新增数据包生成
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存数据包生成
     */
    @RequiresPermissions("wenyanwen:generate:add")
    @Log(title = "数据包生成", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(DbGenerate dbGenerate) {
        return toAjax(dbGenerateService.insertDbGenerate(dbGenerate));
    }

    /**
     * 修改数据包生成
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        DbGenerate dbGenerate = dbGenerateService.selectDbGenerateById(id);
        mmap.put("dbGenerate", dbGenerate);
        return prefix + "/edit";
    }

    /**
     * 修改保存数据包生成
     */
    @RequiresPermissions("wenyanwen:generate:edit")
    @Log(title = "数据包生成", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(DbGenerate dbGenerate) {
        return toAjax(dbGenerateService.updateDbGenerate(dbGenerate));
    }

    @RequiresPermissions("wenyanwen:generate:edit")
    @GetMapping("/progress/{id}")
    @ResponseBody
    public AjaxResult progress(@PathVariable("id") Long id) {
        DbGenerate dbGenerate = dbGenerateService.selectDbGenerateById(id);

        String progress = String.format(PROGRESS_TEMPLATE, DbGenerateUtil.getProgressStyle(dbGenerate), dbGenerate.getProgress());

        Map<String, Object> result = new HashMap<String, Object>();
        result.put("progress", progress);
        result.put("result", DbGenerateUtil.getResult(dbGenerate));
        result.put("status", DbGenerateUtil.getStatus(dbGenerate));
        result.put("st", dbGenerate.getStatus());

        return AjaxResult.success(result);
    }

    /**
     * 删除数据包生成
     */
    @RequiresPermissions("wenyanwen:generate:remove")
    @Log(title = "数据包生成", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(dbGenerateService.deleteDbGenerateByIds(ids));
    }


    /**
     * 数据打包
     */
    @RequiresPermissions("wenyanwen:generate:remove")
    @Log(title = "数据打包", businessType = BusinessType.DELETE)
    @PostMapping("/addTask")
    @ResponseBody
    public AjaxResult addTask(String dbName, String command) {
        String username = ShiroUtils.getLoginName();
        return toAjax(dbGenerateService.addTask(dbName, command, username));
    }
}
